﻿namespace WindowsPhone8App
{
    using System;
    using System.Device.Location;
    using System.Reactive.Linq;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;

    public partial class MainPage : PhoneApplicationPage
    {
        private double distance = 0.0;
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Loaded -= MainPage_Loaded;

            var geoCoordinateService = App.GeoCoordinateService;

            geoCoordinateService.StatusObservable
                .ObserveOnDispatcher()
                .Subscribe(this.OnStatusChanged);

            geoCoordinateService.PositionObservable
                .ObserveOnDispatcher()
                .Subscribe(this.OnPositionChanged);

            geoCoordinateService.PositionObservable
                .Zip(
                    geoCoordinateService.PositionObservable.Skip(1),
                    (p1, p2) => p1.Location.GetDistanceTo(p2.Location))
                .ObserveOnDispatcher()
                .Subscribe(this.OnDistanceChanged);

            this.startApplicationBarIconButton = this.ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            this.stopApplicationBarIconButton = this.ApplicationBar.Buttons[1] as ApplicationBarIconButton;
            this.resetApplicationBarIconButton = this.ApplicationBar.Buttons[2] as ApplicationBarIconButton;

            this.resetApplicationBarIconButton.IsEnabled = true;
            this.startApplicationBarIconButton.IsEnabled = true;

            this.DisplayStatus("Stopped");

            this.DisplayDistance();
        }

        private void OnStatusChanged(GeoPositionStatus status)
        {
            System.Diagnostics.Debug.WriteLine(status);

            string statusText = string.Format("{0}", status);

            this.DisplayStatus(statusText);
        }

        private void OnPositionChanged(GeoPosition<GeoCoordinate> position)
        {
            System.Diagnostics.Debug.WriteLine(position.Location);

            this.locationTextBlock.Text = string.Format("{0}", position.Location);
        }

        private void OnDistanceChanged(double distance)
        {
            System.Diagnostics.Debug.WriteLine(distance);

            this.distance += distance;

            this.DisplayDistance();
        }

        private void Start_Click(object sender, System.EventArgs e)
        {
            App.GeoCoordinateService.Start();

            this.startApplicationBarIconButton.IsEnabled = false;
            this.stopApplicationBarIconButton.IsEnabled = true;
        }

        private void Stop_Click(object sender, System.EventArgs e)
        {
            App.GeoCoordinateService.Stop();

            this.DisplayStatus("Stopped");
            this.stopApplicationBarIconButton.IsEnabled = false;
            this.startApplicationBarIconButton.IsEnabled = true;
        }

        private void Reset_Click(object sender, System.EventArgs e)
        {
            this.distance = 0;

            this.DisplayDistance();
        }

        private void DisplayStatus(string statusText)
        {
            this.statusTextBlock.Text = statusText;
        }

        private void DisplayDistance()
        {
            this.distanceTextBlock.Text = string.Format("{0}m", this.distance);
        }
    }
}